﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DC2010.Objects;
using DC2010;
using DC2010_Editor.Properties;
using DC2010.Tools;
using DC2010.Objects.Base;
using log4net;
using log4net.Config;
using System.Drawing.Drawing2D;
using DC2010_Editor.Controls.Utils;

namespace DC2010_Editor.Controls
{
    public partial class MapEditor : UserControl
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(MapEditor));

        int X = 0;
        int Y = 0;

        int MapCoordinateX = 0;
        int MapCoordinateY = 0;

        public MapEditor()
        {
            InitializeComponent();

            Init();
            DOMConfigurator.Configure();


            ImageListBoxItem itmClear = new ImageListBoxItem("Clear", Resources.Clear, "Clear");
            ImageListBoxItem itmWall = new ImageListBoxItem("Wall", Resources.Wall, "Wall");
            ImageListBoxItem itmFakeWall = new ImageListBoxItem("Fake wall", Resources.FakeWall, "FakeWall"); 
            ImageListBoxItem itmFloor = new ImageListBoxItem("Floor", Resources.Floor, "Floor");
            ImageListBoxItem itmPit = new ImageListBoxItem("Pit (open)", Resources.Pit, "Pit");
            ImageListBoxItem itmPitClosed = new ImageListBoxItem("Pit (closed)", Resources.PitClosed, "PitClosed");
            ImageListBoxItem itmPitIllusion = new ImageListBoxItem("Pit (Looks always open))",Resources.PitIllusion,"PitIllusion");
            ImageListBoxItem itmPitConcealed = new ImageListBoxItem("Pit (Illusion - only outlined)", Resources.PitConcealed, "PitConcealed");
            
            ImageListBoxItem itmPreasurePad = new ImageListBoxItem("Preasure pad", Resources.PreasurePad,"PreasurePad");
            ImageListBoxItem itmPreasurePadInvisible = new ImageListBoxItem("Preasure pad (Invisible)", Resources.PreasurePadIllusion, "PreasurePadInvisible");
            ImageListBoxItem itmDoorNS = new ImageListBoxItem("Door (North - South)",Resources.DoorNS,"DoorNS");
            ImageListBoxItem itmDoorWE = new ImageListBoxItem("Door (West - East)",Resources.DoorWE,"DoorWE");
            ImageListBoxItem itmStairsUPNS = new ImageListBoxItem("Stairs Up (Facing from N)",Resources.StairsNS,"StairsUPNS");
            ImageListBoxItem itmStairsUPEW = new ImageListBoxItem("Stairs Up (Facing from E)",Resources.StairsEW,"StairsUPEW");
            ImageListBoxItem itmStairsUPSN = new ImageListBoxItem("Stairs Up (Facing from S)",Resources.StairsSN,"StairsUPSN");
            ImageListBoxItem itmStairsUPWE = new ImageListBoxItem("Stairs Up (Facing from W)",Resources.StairsWE,"StairsUPWE");
            ImageListBoxItem itmStairsDOWNNS = new ImageListBoxItem("Stairs Down (Facing from N)", Resources.StairsSN, "StairsDOWNNS");
            ImageListBoxItem itmStairsDOWNEW = new ImageListBoxItem("Stairs Down (Facing from E)", Resources.StairsWE, "StairsDOWNEW");
            ImageListBoxItem itmStairsDOWNSN = new ImageListBoxItem("Stairs Down (Facing from S)", Resources.StairsNS, "StairsDOWNSN");
            ImageListBoxItem itmStairsDOWNWE = new ImageListBoxItem("Stairs Down (Facing from W)", Resources.StairsEW, "StairsDOWNWE");

            ImageListBoxItem itmTeleportBlue = new ImageListBoxItem("Blue haze", Resources.TeleportBlue, "TeleportBlue");
            ImageListBoxItem itmTeleportGreen = new ImageListBoxItem("Green haze", Resources.TeleportGreen, "TeleportGreen");
            ImageListBoxItem itmTeleportRed = new ImageListBoxItem("Red haze", Resources.TeleportRed, "TeleportRed");
            ImageListBoxItem itmPartyStart = new ImageListBoxItem("Party start", Resources.PartyStart, "PartyStart");


            imageListBox1.Items.Add(itmClear);
            imageListBox1.Items.Add(itmWall);
            imageListBox1.Items.Add(itmFakeWall);
            imageListBox1.Items.Add(itmFloor);
            imageListBox1.Items.Add(itmPit);
            imageListBox1.Items.Add(itmPitClosed);
            imageListBox1.Items.Add(itmPitIllusion);
            imageListBox1.Items.Add(itmPitConcealed);

            imageListBox1.Items.Add(itmPreasurePad);
            imageListBox1.Items.Add(itmPreasurePadInvisible);
            imageListBox1.Items.Add(itmDoorNS);
            imageListBox1.Items.Add(itmDoorWE);
            imageListBox1.Items.Add(itmStairsUPNS);
            imageListBox1.Items.Add(itmStairsUPEW);
            imageListBox1.Items.Add(itmStairsUPSN);
            imageListBox1.Items.Add(itmStairsUPWE);
            imageListBox1.Items.Add(itmStairsDOWNNS);
            imageListBox1.Items.Add(itmStairsDOWNEW);
            imageListBox1.Items.Add(itmStairsDOWNSN);
            imageListBox1.Items.Add(itmStairsDOWNWE);

            imageListBox1.Items.Add(itmTeleportBlue);
            imageListBox1.Items.Add(itmTeleportGreen);
            imageListBox1.Items.Add(itmTeleportRed);
            imageListBox1.Items.Add(itmPartyStart);

            Editor.I.CurrentMapId = "Test_Level";
        }

        public void Init()
        {
            //Root.I.Init();

            Origin = new Point(20, 20);
            BoxSize = 16;

            Refresh();

           // displayMapControl1.MouseMove += new MouseEventHandler(displayMapControl1_MouseMove);

        }

        void displayMapControl1_MouseMove(object sender, MouseEventArgs e)
        {
           // displayMapControl1.Refresh();
        }

        public void Refresh()
        {
            ItemsImageListBox.Items.Clear();
            comboBox1.Items.Clear();

            foreach (Item i in Root.I.ItemSystem.ListInstance)
            {
                BaseObject c = i.FindOwner();
                if (c == null)
                {
                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.Instanced, i);
                    ItemsImageListBox.Items.Add(itm);
                }
            }

            foreach (Map m in Root.I.MapSystem.ListInstance)
            {
                comboBox1.Items.Add(m.Info.Id);
            }
        }

        private Point Origin { get; set; }
        private int BoxSize { get; set; }

        private void EditorPictureBox_Paint(object sender, PaintEventArgs e)
        {
            //e.Graphics.FillRectangle(new SolidBrush(Color.Black), e.ClipRectangle);

            int noofboxesx = (e.ClipRectangle.Width / BoxSize) - 2;
            int noofboxesy = (e.ClipRectangle.Height / BoxSize) - 2;

            DrawGrid(e.Graphics, Origin, BoxSize, noofboxesy, noofboxesx);

            DrawCursor(e.Graphics);



            //first wath if there is something to fetch from
            Map m = Root.I.IS.GetMap(Editor.I.CurrentMapId);
            if (m!=null)
                DrawMap(e.Graphics, Origin, BoxSize, m);
        }

        private void DrawMap(Graphics graphics, Point origin, int boxsize, Map m)
        {
            for (int x = 0; x <= m.X; x++)
            {
                for (int y = 0; y <= m.Y; y++)
                {
                    try
                    {
                        Tile t = m[x, y];
                        if (t == null) break;

                        if (t != null)
                        {
                            // draw floor
                            if (t is ClearFloorTile)
                            {
                                if (!t.Info.IsWall)
                                {
                                    bool bItIsTeleport = false;

                                    //check for teleport effect
                                    foreach (SimpleEffect se in t.Info.OnEnterEffects)
                                    {
                                        if (se.Id == "I_TELEPORT") bItIsTeleport = true;
                                    }

                                    if (!bItIsTeleport)
                                        graphics.DrawImage(Properties.Resources.Floor, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));
                                    else
                                        graphics.DrawImage(Properties.Resources.TeleportBlue, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                }

                                if (t.Info.IsWall)
                                {
                                    //is it fake?
                                    if (t.Info.CanPlayerPass)
                                        graphics.DrawImage(Properties.Resources.FakeWall, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));
                                    else
                                        graphics.DrawImage(Properties.Resources.Wall, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));
                                }                                    
                            }

                            //draw pit
                            if (t is PitTile)
                            {
                                PitTile pt = (PitTile)t;

                                if (pt.IsOpen)
                                    graphics.DrawImage(Properties.Resources.Pit, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if (!pt.IsOpen)
                                    graphics.DrawImage(Properties.Resources.PitClosed, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if (pt.IsConcealed)
                                    graphics.DrawImage(Properties.Resources.PitConcealed, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if (pt.IsIllusionary)
                                    graphics.DrawImage(Properties.Resources.PitIllusion, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));
                            }

                            if (t is StairsTile)
                            {
                                StairsTile st = (StairsTile)t;

                                StairsDirection sd = st.Direction;
                                TileVector tv = st.TileVector;

                                if ((sd == StairsDirection.Up) && (tv == TileVector.EW))
                                    graphics.DrawImage(Properties.Resources.StairsEW, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if ((sd == StairsDirection.Up) && (tv == TileVector.NS))
                                    graphics.DrawImage(Properties.Resources.StairsNS, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if ((sd == StairsDirection.Up) && (tv == TileVector.SN))
                                    graphics.DrawImage(Properties.Resources.StairsSN, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if ((sd == StairsDirection.Up) && (tv == TileVector.WE))
                                    graphics.DrawImage(Properties.Resources.StairsWE, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if ((sd == StairsDirection.Down) && (tv == TileVector.EW))
                                    graphics.DrawImage(Properties.Resources.StairsWE, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if ((sd == StairsDirection.Down) && (tv == TileVector.NS))
                                    graphics.DrawImage(Properties.Resources.StairsSN, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if ((sd == StairsDirection.Down) && (tv == TileVector.SN))
                                    graphics.DrawImage(Properties.Resources.StairsNS, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                                if ((sd == StairsDirection.Down) && (tv == TileVector.WE))
                                    graphics.DrawImage(Properties.Resources.StairsEW, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));

                            }

                            //draw door
                            if (t is DoorTile)
                            {
                                DoorDirection dd = DoorDirection.NS;

                                if (t is DoorTile) dd = ((DoorTile)t).Direction;

                                if (dd == DoorDirection.NS)
                                {
                                    graphics.DrawImage(Properties.Resources.DoorNS, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));
                                }

                                if (dd == DoorDirection.WE)
                                {
                                    graphics.DrawImage(Properties.Resources.DoorWE, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));
                                }
                            }
                        }

                        if ((m.Info.StartPosition.X == x) && (m.Info.StartPosition.Y == y))
                        {
                            graphics.DrawImage(Properties.Resources.PartyStart, new Point(x * boxsize + origin.X, y * boxsize + origin.Y));
                        }

                        

                        graphics.DrawImage(Properties.Resources.Wall, new Point((m.X + 1) * boxsize + origin.X, y * boxsize + origin.Y));

                    }
                    catch(Exception ex)
                    {
                        int a=9;
                    }
                    graphics.DrawImage(Properties.Resources.Wall, new Point(x * boxsize + origin.X, (m.Y + 1) * boxsize + origin.Y));
                }


            }

            Tile tx = m[MapCoordinateX, MapCoordinateY];
            DrawAssociations(graphics, tx, MapCoordinateX, MapCoordinateY);
            
        }

        private void DrawCursor(Graphics graphics)
        {
            this.ParentForm.Text = string.Format("{0:00} : {1:00}", MapCoordinateX, MapCoordinateY);
        }

        public void DrawGrid(Graphics g, Point origin, int boxSize, int noOfBoxesX, int noOfBoxesY)
        {
            Pen p = new Pen(Color.FromArgb(64,64,64));
            for (int i = 0; i <= noOfBoxesX; i++)
            {
                g.DrawLine(p, new Point(origin.X, origin.Y + i * boxSize), new Point(origin.X + noOfBoxesY * boxSize, origin.Y + i * boxSize));
            }

            for (int i = 0; i <= noOfBoxesY; i++)
            {
                g.DrawLine(p, new Point(origin.X + i * boxSize, origin.Y), new Point(origin.X + i * boxSize, origin.Y + noOfBoxesX * boxSize));
            }
        }

        public void DrawAssociations(Graphics g, Tile t, int x, int y)
        {

            g.SmoothingMode = SmoothingMode.AntiAlias;
            foreach (string s in t.Info.LinkedObjects)
            {
                // TODO
            }

            foreach (DungeonPlacement dp in t.Info.Items)
            {                
                BaseObject bo = Root.I.IS.GetLinkedObject(dp.ObjectId);

                if (bo is Item)
                {
                    Item i = (Item)bo;
                    foreach (string s in i.Info.LinkedObjects)
                    {
                        DungeonPlacement dpLinked = Root.I.GameSystem.FindLinkedObjectLocation(s);

                        if (dpLinked != null)
                        {
                            Point pFrom = new Point(0, 0);
                            Point pTo = new Point(0, 0);
                            Pen p = new Pen(Color.LimeGreen, 2.0f);
                            Pen pThick = new Pen(Color.Green, 4f);

                            if (dp.ObjectPosition == ObjectPosition.East)
                            {
                                pFrom = new Point((x * BoxSize) + Origin.X + 1 * BoxSize, (y * BoxSize) + Origin.Y + BoxSize/2);
                            }

                            if (dp.ObjectPosition == ObjectPosition.South)
                            {
                                pFrom = new Point((x * BoxSize) + Origin.X + 1 * BoxSize - BoxSize / 2, (y * BoxSize) + Origin.Y + BoxSize);
                            }

                            if (dpLinked.ObjectPosition == ObjectPosition.Center)
                            {
                                pTo = (Point)dpLinked.Tag;
                                pTo.X = pTo.X * BoxSize + Origin.X + BoxSize/2;
                                pTo.Y = pTo.Y * BoxSize + Origin.Y + BoxSize/2;
                            }

                            g.DrawLine(pThick, pFrom, pTo);
                            g.DrawLine(p, pFrom, pTo);
                        }
                    }
                }
            }
        }


        private void EditorPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            MapCoordinateX = ((e.X - Origin.X) / BoxSize);
            MapCoordinateY = ((e.Y - Origin.Y) / BoxSize);

            try
            {
                EditorPictureBox.Invalidate();
                Map m = Root.I.IS.GetMap(Editor.I.CurrentMapId);
                Tile t = m[MapCoordinateX, MapCoordinateY];

                if (t != null)
                {
                    if (t.Info != null)
                    {
                        if (t.Info.Id != null)
                        {
                            label5.Text = t.Info.Id.ToString();
                        }
                        else
                        {
                            label5.Text = "";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                int a = 9;
            }
        }

        private void EditorPictureBox_Click(object sender, EventArgs e)
        {
            ImageListBoxItem itm = (ImageListBoxItem)imageListBox1.SelectedItem;

            if (itm != null)
            {
                try
                {
                    string typ = itm.Tag.ToString();

                    switch(typ)
                    {
                        case "Clear":
                            {
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = null;
                                break; 
                            }

                        case "Floor":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.CanHoldItems = true;
                                dt.Info.CanMonsterPass = true;
                                dt.Info.CanPlayerPass = true;
                                dt.Info.Id = string.Format("TILE_FLOOR_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "Wall":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.IsWall = true;
                                dt.Info.CanHoldItems = true;
                                dt.Info.CanMonsterPass = false;
                                dt.Info.CanPlayerPass = false;
                                dt.Info.Id = string.Format("TILE_WALL_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "FakeWall":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.IsWall = true;
                                dt.Info.CanHoldItems = true;
                                dt.Info.CanMonsterPass = true;
                                dt.Info.CanPlayerPass = true;
                                dt.Info.Id = string.Format("TILE_WALL_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "Pit":
                            {
                                PitTile dt = new PitTile();
                                dt.Info.Id = string.Format("TILE_PIT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.IsOpen = true;
                                dt.IsIllusionary = false;
                                dt.IsConcealed = false;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "PitClosed":
                            {
                                PitTile dt = new PitTile();
                                dt.Info.Id = string.Format("TILE_PIT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.IsOpen = false;
                                dt.IsIllusionary = false;
                                dt.IsConcealed = false;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "PitIllusion":
                            {
                                PitTile dt = new PitTile();
                                dt.Info.Id = string.Format("TILE_PIT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.IsOpen = true;
                                dt.IsIllusionary = true;
                                dt.IsConcealed = false;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "PitConcealed":
                            {
                                PitTile dt = new PitTile();
                                dt.Info.Id = string.Format("TILE_PIT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                dt.IsOpen = true;
                                dt.IsIllusionary = false;
                                dt.IsConcealed = true;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "PreasurePad":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.Id = string.Format("TILE_PREASSURE_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;

                            }
                        case "PreasurePadInvisible":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.Id = string.Format("TILE_PREASSURE_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;

                            }
                        case "DoorWE":
                            {
                                Door d = Root.I.DoorSystem.Dict["DOOR_WOODEN"];
                                Door dCloned = (Door)d.Clone();
                                dCloned.Info.Id = string.Format("DOOR_WOODEN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dCloned.Info.TemplateId = "DOOR_WOODEN";

                                DoorTile dt = new DoorTile();
                                dt.Info.Id = string.Format("TILE_DOOR_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.DoorId = dCloned.Info.Id;

                                dt.Direction = DoorDirection.WE;


                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.DoorSystem.AddToSystem(dCloned, dCloned.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "DoorNS":
                            {
                                Door d = Root.I.DoorSystem.Dict["DOOR_WOODEN"];
                                Door dCloned = (Door)d.Clone();
                                dCloned.Info.Id = string.Format("DOOR_WOODEN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dCloned.Info.TemplateId = "DOOR_WOODEN";

                                DoorTile dt = new DoorTile();
                                dt.Info.Id = string.Format("TILE_DOOR_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.Direction = DoorDirection.NS;
                                dt.DoorId = dCloned.Info.Id;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.DoorSystem.AddToSystem(dCloned, dCloned.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "StairsUPNS":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_UP_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.NS;
                                st.Direction = StairsDirection.Up;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsUPEW":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_UP_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.EW;
                                st.Direction = StairsDirection.Up;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsUPSN":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_UP_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.SN;
                                st.Direction = StairsDirection.Up;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsUPWE":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_UP_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.WE;
                                st.Direction = StairsDirection.Up;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsDOWNNS":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_DOWN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.NS;
                                st.Direction = StairsDirection.Down;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsDOWNEW":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_DOWN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.EW;
                                st.Direction = StairsDirection.Down;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsDOWNSN":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_DOWN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.SN;
                                st.Direction = StairsDirection.Down;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsDOWNWE":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_DOWN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.WE;
                                st.Direction = StairsDirection.Down;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "PartyStart":
                            {
                                Map m = Root.I.IS.GetMap(Editor.I.CurrentMapId);
                                //m.Info.StartPosition = new Microsoft.Xna.Framework.Point(MapCoordinateX, MapCoordinateY);
                                break;
                            }
                        case "TeleportBlue":
                            {
                                ClearFloorTile cft = new ClearFloorTile();
                                cft.Info.CanHoldItems = true;
                                cft.Info.CanMonsterPass = true;
                                cft.Info.CanPlayerPass = true;
                                cft.Info.Id = string.Format("TILE_TELEPORT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                cft.Info.OnEnterEffects.Add(new SimpleEffect("I_TELEPORT", 1, 1, 0, 0, 0, Editor.I.CurrentMapId, null, null, null, null));
                                cft.Info.OnEnterEffects.Add(new SimpleEffect("I_PLAY_SOUND", 0, 0, 0, 0, 0, "teleport", null, null, null, null));
                                
                                Root.I.TileSystem.AddToSystem(cft, cft.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = cft;
                                break;

                            }

                    }
                }
                catch (Exception ex)
                {
                    int a = 9;
                }
            }

            Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Root.I.MapSystem.Save();
            Root.I.MapSystem.SaveInstance();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CreateNewMapForm cnmf = new CreateNewMapForm(OpenType.Create, null);
            cnmf.ShowDialog();

            Refresh();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //save current map
            Root.I.MapSystem.SaveInstance();

            Editor.I.CurrentMapId = comboBox1.Text;

            EditorPictureBox.Invalidate();


        }

        private void crystalButton3_Click(object sender, EventArgs e)
        {
            Map m = Root.I.IS.GetMap(Editor.I.CurrentMapId);
            CreateNewMapForm cnmf = new CreateNewMapForm(OpenType.Edit, m);
            cnmf.ShowDialog();

            Refresh();
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImageListBoxItem itm = (ImageListBoxItem)imageListBox1.SelectedItem;

            if (itm != null)
            {
                Editor.I.ActiveTileType = itm.Tag.ToString();
            }
        }

    }
}
